package cn.rwklyd.BookKeeping.controller;

import cn.rwklyd.BookKeeping.pojo.BudgetCategory;
import cn.rwklyd.BookKeeping.pojo.Result;
import cn.rwklyd.BookKeeping.pojo.vo.BudgetCategoryVO;
import cn.rwklyd.BookKeeping.service.BudgetCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/budgetCategory")
@Tag(name = "预算分类管理", description = "预算分类的增删查改接口")
@Slf4j
public class BudgetCategoryController {

    @Autowired
    private BudgetCategoryService budgetCategoryService;

    @PostMapping("/add")
    @Operation(summary = "添加预算分类", description = "添加一个新的预算分类")
    public Result<String> addBudgetCategory(@RequestBody BudgetCategory budgetCategory) {
        log.info("添加预算分类: {}", budgetCategory);
        return budgetCategoryService.addBudgetCategory(budgetCategory);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除预算分类", description = "根据ID删除预算分类")
    public Result<String> deleteBudgetCategory(@Parameter(description = "预算分类ID", required = true) @PathVariable Long id) {
        log.info("删除预算分类: {}", id);
        return budgetCategoryService.deleteBudgetCategory(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新预算分类", description = "更新预算分类信息")
    public Result<String> updateBudgetCategory(@RequestBody BudgetCategory budgetCategory) {
        log.info("更新预算分类: {}", budgetCategory);
        return budgetCategoryService.updateBudgetCategory(budgetCategory);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取预算分类", description = "根据ID获取预算分类详情")
    public Result<BudgetCategory> getBudgetCategoryById(@Parameter(description = "预算分类ID", required = true) @PathVariable Long id) {
        log.info("获取预算分类: {}", id);
        return budgetCategoryService.getBudgetCategoryById(id);
    }

    @GetMapping("/list")
    @Operation(summary = "查询预算分类列表", description = "根据预算ID查询预算分类列表")
    public Result<IPage<BudgetCategory>> listBudgetCategories(
            @Parameter(description = "预算ID", required = true) @RequestParam Long budgetId,
            @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询预算分类列表: budgetId={}, page={}, pageSize={}", budgetId, page, pageSize);
        return budgetCategoryService.listBudgetCategories(budgetId, page, pageSize);
    }
    
    @GetMapping("/listWithExpense")
    @Operation(summary = "查询预算分类列表（含消费总额）", description = "根据预算ID查询预算分类列表，并包含本月消费总额")
    public Result<IPage<BudgetCategoryVO>> listBudgetCategoriesWithExpense(
            @Parameter(description = "预算ID", required = true) @RequestParam Long budgetId,
            @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询预算分类列表（含消费总额）: budgetId={}, page={}, pageSize={}", budgetId, page, pageSize);
        return budgetCategoryService.listBudgetCategoriesWithExpense(budgetId, page, pageSize);
    }
} 